﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EffiProz.Core.Lib;
using EffiProz.Core.Persist;
using EffiProz.Core.RowIO;
using EffiProz.Core.Indexes;
using EffiProz.Core.Errors;
using EffiProz.Core.Rights;
using EffiProz.Core.Navigators;
using EffiProz.Core.DataTypes;
using EffiProz.Core.Results;

namespace EffiProz.Core
{
    public class TriggerDefSQL : TriggerDef
    {
        OrderedHashSet references;

        public TriggerDefSQL(QNameManager.QName name, String when,
                             String operation, bool forEachRow, Table table,
                             Table[] transitions, RangeVariable[] rangeVars,
                             Expression condition, String conditionSQL,
                             int[] updateColumns,
                             StatementDMQL[] compiledStatements,
                             String procedureSQL, OrderedHashSet references)
            : base(name, when, operation, forEachRow, table, transitions,
                  rangeVars, condition, conditionSQL, updateColumns)
        {



            this.statements = compiledStatements;
            this.procedureSQL = procedureSQL;
            this.references = references;
        }

        public override OrderedHashSet getReferences()
        {
            return references;
        }

        public override OrderedHashSet getComponents()
        {
            return null;
        }

        public override void compile(Session session, SchemaObject parentObject) { }

        public override String getClassName()
        {
            return null;
        }

        public override bool hasOldTable()
        {
            return transitions[OLD_TABLE] != null;
        }

        public override bool hasNewTable()
        {
            return transitions[NEW_TABLE] != null;
        }

        public override void pushPair(Session session, Object[] oldData,
                                   Object[] newData)
        {
            lock (this)
            {
                if (transitions[OLD_ROW] != null)
                {
                    rangeVars[OLD_ROW].getIterator(session).currentData = oldData;
                }

                if (transitions[NEW_ROW] != null)
                {
                    rangeVars[NEW_ROW].getIterator(session).currentData = newData;
                }

                if (!condition.testCondition(session))
                {
                    return;
                }

                for (int i = 0; i < statements.Length; i++)
                {
                    statements[i].execute(session);
                }
            }
        }

        public override String getSQL()
        {

            bool isBlock = statements.Length > 1;
            StringBuilder sb = getSQLMain();

            if (isBlock)
            {
                sb.Append(Tokens.T_BEGIN).Append(' ').Append(Tokens.T_ATOMIC);
                sb.Append(' ');
            }

            sb.Append(procedureSQL).Append(' ');

            if (isBlock)
            {
                sb.Append(Tokens.T_END);
            }

            return sb.ToString();
        }
    }
}
